package com.cjzuji.rental.dao.impl;

import com.cjzuji.rental.dao.AiDouBillDao;
import com.cjzuji.rental.mapper.OrderBillMapper;
import com.cjzuji.rental.pojo.OrderBill;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class AiDouBillDaoImpl implements AiDouBillDao {
    private static final Logger logger = LoggerFactory.getLogger(AiZuJiBillDaoImpl.class);
    @Autowired
    OrderBillMapper orderBillMapper;

    @Override
    @Transactional
    public  void save(List<Map<String,Object>> billdata, Long orderID) {
        List<OrderBill>  billList = new ArrayList<>();
        for (Map<String, Object> item : billdata){
            //logger.info("账单信息添加开始{}", item.toString());
            String repaymentTime = "";
            if(item.get("actualTime") != null){
                repaymentTime = item.get("actualTime").toString();
            }
            String payStatus = "未支付";
            if ("PAID".equals(item.get("payStatus").toString())){
                payStatus = "已支付";
            }else if("OVERDUE_PAID".equals(item.get("payStatus").toString())){
                payStatus = "逾期支付";
            }
            Double realPayCapital = 0.0;
            if (item.get("actualAmount") != null){
                realPayCapital = Double.parseDouble(item.get("actualAmount").toString());
            }
            OrderBill orderBill = OrderBill.builder()
                    .orderId(orderID)
                    .ordersId(item.get("orderSn").toString())
                    .realPayTime(repaymentTime)  //还款时间
                    .term((int) item.get("periods"))   //租期
                    .realPayCapital(realPayCapital)  //实付金额
                    .capital((Double) item.get("invoiceAmount"))      //应还金额
                    .monthRepayment((Double)item.get("invoiceAmount"))  //月租金
                    .zfbDiscountAmt(0.0)  //支付宝优惠金额
                    .ovdueBuyoutAmt(Double.parseDouble(item.get("allinAmount").toString()))     //逾期买断金
                    .writeOffAmt(0.0)      //优惠金额
                    .reduceAmt(0.0)      //减免金额
                    .overdueAmount(0.0)       //逾期违约金
                    .repayDateFmt(item.get("estimatedTime").toString())  //还款日
                    .realPayTradeId("") // 商家交易号
                    .repayStatusStr(payStatus)  //还款状态
                    .realRepayTime(repaymentTime)  //还款时间的
                    .createTime(new Date())
                    .updateTime(new Date())
                    .build();
            //logger.info("打印添加参数2 {}",orderBill);
            billList.add(orderBill);
        }

        try {
            System.out.println("批量保存：~~~~~~~~~~"+ billList.toString());
            orderBillMapper.insertBatchSomeColumn(billList);
        }catch (Exception e){
            logger.info("账单信息添加错误" + e.getMessage());
            throw e; // 重新抛出，确保事务回滚
        }
    }

    @Override
    public List<OrderBill> getOrderBillList(Long orderId) {
        return orderBillMapper.getOrderBillList(orderId);
    }
}

